function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 在Object原型链上添加watch方法
 */
function addWatchFun() {
  if (!Object.prototype.watch) {
    Object.prototype.watch = function (prop, handler) {
      var oldval = this[prop], newval = oldval,
        getter = function () {
          return newval;
        },
        setter = function (val) {
          oldval = newval;
          return newval = handler.call(this, prop, oldval, val);
        };
      if (delete this[prop]) {
        if (Object.defineProperty) // ECMAScript 5
        {
          Object.defineProperty(this, prop, { get: getter, set: setter });
        }
        else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) {
          Object.prototype.__defineGetter__.call(this, prop, getter);
          Object.prototype.__defineSetter__.call(this, prop, setter);
        }
      }
    };
  }

  if (!Object.prototype.unwatch) {
    Object.prototype.unwatch = function (prop) {
      var val = this[prop];
      delete this[prop];
      this[prop] = val;
    };
  }
}

function getMac(mac) {
  if (/^[0-9]{10}$/.test(mac)) {
    if (mac.length === 10) {
      return mac
    }
  } else if (/^http(s)?:\/\/[\w.-]+\/qrcode\?mac=[0-9]{10}$/.test(mac)) {
    var mac1 = res.result.substr(mac.indexOf('mac=') + 'mac='.length)
    if (mac1.length === 10) {
      return mac1
    }

  }
  return null
}

module.exports = {
  formatTime: formatTime,
  addWatchFun: addWatchFun,
  getMac: getMac
}
